<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * HTML pages
 *
 * @author	Tran Van Thanh
 * @mail	thanhtran@vietnambiz.com
 * @date	06.09.2011
 */
require_once('admin'.EXT);

class Html extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('html_model', 'user_model'));				
	}

	function index()
	{
		$page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1;

        if(isset($_GET['status'])) {
            $status = ($_GET['status'] == 'active') ? 'yes' : 'no';
            $this->db->where('status', $status);
        }

		$this->data['html'] 	= $this->html_model->get_html_pages($page != "" ? $page : 1);		
		$this->data['page'] 	= $page;

    	$conf =	array(
    		'base_url'		=> admin_url($this->router->class).'/',
    		'total_rows'	=> $this->html_model->count_all(),
    		'per_page'		=> $this->config->item('html', 'limit'),
    		'cur_page'		=> $page,
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav'] = $this->pagination->display_query_string();

		if($this->input->is_ajax_request()) {
			$this->load->view('html/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'html/index';
			$this->load->view('layout/default', $this->data);
		}
	}

	function add()
	{		
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
			$this->form_validation->set_message('required', '%s không hợp lệ');
    		$this->form_validation->set_rules('txt_title', 'Tiêu đề', 'required|trim|xss_clean');
    		$this->form_validation->set_rules('txt_content', 'Nội dung', 'required|trim|xss_clean');    		                    

    		if($this->form_validation->run() == TRUE)
            {            	
            	$title = $this->input->post('txt_title');

            	$data = array(
            		'title'				=> strip_tags($title),
            		'title_link'		=> ascii_link($title,'-'),
            		'contents'			=> $this->input->post('txt_content'),            		          		
            		'status'			=> $this->input->post('status')
            	);

            	$this->html_model->insert($data);            	
            	die('yes');            	
            }
            else {
            	die(validation_errors());
            }
		}
		
		$this->load->view('html/add', $this->data);
	}

	function edit($html_id = null)
	{					
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
			$this->form_validation->set_message('required', '%s không hợp lệ');
    		$this->form_validation->set_rules('txt_content', 'Nội dung', 'required|trim|xss_clean');    		                      

    		if($this->form_validation->run() == TRUE)
            {                   	
            	$arr = array(
            		'contents'			=> $this->input->post('txt_content'),            		          		
            		'status'			=> $this->input->post('status')
            	);            	
            	            	
            	$this->html_model->update($html_id,$arr);
            	die('yes');            	            	            	
            }
            else {
            	die(validation_errors());
            }
		}
		
		$html = $this->html_model->get($html_id);
		$this->data['html'] 	= $html;		
		$this->load->view('html/edit', $this->data);
	}

    function delete($html_id = null)
    {    	
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {        	
            $this->html_model->delete_html_pages($html_id);         
            die('yes');
        }
    }

	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}
			elseif($action == 'pending') {
				$this->html_model->update_many($id_list, array('status' => 'no'));
			}
			elseif($action == 'active') {
				$this->html_model->update_many($id_list, array('status' => 'yes'));
			}

			die('yes');
		}
	}

	function change_status()
	{
		$html_id = $this->input->post('html_id');
		$status = $this->input->post('status');

		$this->html_model->update($html_id, array('status' => $status));
		die('yes');
	}

	function load_row($html_id = null)
	{		
		$this->data['html'] = $this->html_model->get($html_id);
		$this->load->view('html/row', $this->data);
	}	
}

